/**
 * The Footbar global namespace
 * @class Footbar.TabWatcher
 * @static
 */
if (typeof Footbar == "undefined") {
    Footbar = {};
}

Footbar.TabWatcher={
    lastSelectedTab:null,
    showWarnings:false,//Show alert messages
    activeTabArr:[],
    lastActiveTabArr:[],
/**
 * Initalize tabwatcher
 */
    initalize:function(){
        
        //Initialize the Toolbars
        var toolbox=document.getElementsByTagName('toolbox');
        for(var m=0;m<toolbox.length;m++){
            var toolbar=toolbox[m];
            var toolbarButtons=toolbar.getElementsByTagName("button");
            for(var i=0;i<toolbarButtons.length;i++){
                var button=toolbarButtons[i];
                if(!button.hasAttribute("class") || !button.getAttribute("class")=="tab"){
                    continue;
                }
                var buttonEvent = {
                    handleEvent: function(e) {
                        for(var k=0;k<this.collection.length;k++){
                            this.collection[k].setAttribute("selected", false);
                        }
                        this.object.setAttribute("selected", true);
                        Footbar.TabWatcher.setFocus(this.parent, this.index);
                    },
                    index:i,
                    object:button,
                    collection:toolbarButtons,
                    parent:toolbar
                }
                button.addEventListener("click", buttonEvent, false);

                var buttonBlur = {
                    handleEvent: function(e){
                        var msg="Lost Focus :"+this.index +" > "+Footbar.TabWatcher.lastSelectedTab;
                        msg+="\nFootbar.TabWatcher.lastSelectedTab="+Footbar.TabWatcher.lastSelectedTab.owner;
                        msg+="\nFootbar.TabWatcher.lastSelectedTab="+Footbar.TabWatcher.lastSelectedTab.index;
                        //dump("\n"+msg);
                        Footbar.TabWatcher.setLastActiveTab(Footbar.TabWatcher.lastSelectedTab.owner, Footbar.TabWatcher.lastSelectedTab.index);
                    },
                    index:i,
                    object:button
                }
                button.addEventListener("blur", buttonBlur, true);
            }
            //Init to first tab
            Footbar.TabWatcher.setFocus(toolbar, 0);
        }
    },
/**
 *
 * @param {Object} ownerId
 * @param {Object} index
 * @param {Object} context If we are calling this from outside the sidebar we need to pass in sidebar context
 */
    setFocus:function(ownerId, index, context){
        var focusContext=document;
        if(context!=null){
            focusContext=context;
        }
        var toolbar;
        var deckContainer;
        var deckid;
        if(typeof(ownerId)=='string'){
            toolbar=focusContext.getElementById( ownerId );
            deckid=ownerId+"-deck" ;
        }else{
            toolbar=ownerId;
            deckid=ownerId.id+"-deck" ;
        }
        deckContainer=focusContext.getElementById( deckid );
        if(deckContainer==null){
            if(this.showWarnings){
                alert("Unable to find Deck Container > "+deckid);
            }

            return;
        }
        Footbar.TabWatcher.setActiveTab(ownerId.id, index);

        var toolbarButtons=toolbar.getElementsByTagName("button");
        for(var i=0;i<toolbarButtons.length;i++){
            var button=toolbarButtons[i];
            if(!button.hasAttribute("class") || !button.getAttribute("class")=="tab"){
                continue;
            }
            if(index==i){
                //alert("HERE: "+ownerId)
                button.setAttribute("selected", true);
                deckContainer.selectedIndex=i;
                this.lastSelectedTab={
                    index:i,
                    object:button,
                    owner:ownerId.id
                };
            }else{
                button.setAttribute("selected", false);
            }
        }
    },
/**
 *
 * @param {Object || String} ownerId
 * @param {Object} bind Example:
 *
 *
 */
    bindTabs:function(ownerId, bind){
        var toolbar;
        if(typeof(ownerId)=='string'){
            toolbar=document.getElementById( ownerId );
        }else{
            toolbar=ownerId
        }
        var toolbarButtons=toolbar.getElementsByTagName("button");

        for(var i=0;i<toolbarButtons.length;i++){
            var button=toolbarButtons[i];
            if(!bind[i]){
                alert("TabWatcher.bindTabs i="+i+" is null, probalble cause is that new tab have been added\n but the associated deck have not been inserted in XUL")
                return;
            }
            if(bind[i].onClick)	{
                button.addEventListener("click", bind[i].onClick, true);
            }
            if(bind[i].index==null){
                alert("Index Property not set in bindTabs call");
            }
        }
    },

/**
 * Bind function to a single component
 * @param {Object} ownerId
 * @param {Object} bind
 */
    bindSingleTab:function(ownerId, bind){
        var toolbar;
        if(typeof(ownerId)=='string'){
            toolbar=document.getElementById( ownerId );
        }else{
            toolbar=ownerId
        }
         //Unable to bind 
        if(toolbar == null ) return;

     // alert(ownerId +" > " + toolbar + " > "+bind.panelName)

        dump("\n bind.panelName="+bind.panelName)
        if(bind.index==null){
            alert("Index Property not set in bindSingleTab call");
        }
        var toolbarButtons=toolbar.getElementsByTagName("button");
        var button=toolbarButtons[bind.index];
        var prevent=(bind.preventDefault!='undefined')?bind.preventDefault:false;

        if(bind.onClick){
            button.addEventListener("click", bind.onClick, prevent);
        }
        if(bind.onBlur){
            //  button.addEventListener("blur",function(e){bind.onBlur(e);}, prevent);
        }
    },

    setActiveTab:function(ownerId, index){
        this.activeTabArr[ownerId]=index;
        //alert("setActiveTab  = "+ownerId+" : "+this.activeTabArr[ownerId]);
    },
    getActiveTab:function(ownerId){
        if(ownerId==null || ownerId=="undefined"){
            alert("getActiveTab(): argument ownerId can't be null")
            return 0;
        }
        //alert("getActiveTab  INDEX = "+ownerId+" : "+this.activeTabArr[ownerId]);
        return this.activeTabArr[ownerId];
    },

    setLastActiveTab:function(ownerId, index){
        this.lastActiveTabArr[ownerId]=index;
        //alert("setLastActiveTab  = "+ownerId+" : "+this.lastActiveTabArr[ownerId]);
    },
    getLastActiveTab:function(ownerId){
        if(ownerId==null || ownerId=="undefined"){
            alert("getLastActiveTab(): argument ownerId can't be null")
            return 0;
        }
        //alert("getLastActiveTab  INDEX = "+ownerId+" : "+this.lastActiveTabArr[ownerId]);
        return this.lastActiveTabArr[ownerId];
    }
}		
												 
